package database.dao;

import database.util.DaoUtil;
import database.util.SQLPatinet;
import patient.Patient;
import patient.Plec;
import patient.Zawod;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PatientDao {
    private DaoFactory daoFactory;

    public PatientDao(DaoFactory daoFactory) {
        this.daoFactory = daoFactory;
    }

    public List<Patient> getAllPatients() {
        List<Patient> patients = new ArrayList<Patient>();

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = connection.prepareStatement(SQLPatinet.selectAllQueery());
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Patient patient = getPatientByResultSet(resultSet);
                patients.add(patient);
            }
            System.out.println("Pacjenci pobrani z bazy.");
        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement, resultSet);
        }
        return patients;
    }

    public Patient getPatientByResultSet(ResultSet resultSet) throws SQLException {
        int id = resultSet.getInt(SQLPatinet.W_ID_PATIENT);
        String imie = resultSet.getString(SQLPatinet.W_NAME);
        String nazwisko = resultSet.getString(SQLPatinet.W_SURNAME);
        String PlecStr = resultSet.getString(SQLPatinet.W_SEX);
        Plec plec = Plec.valueOf(PlecStr);
        int czyUbezpieczonyInt = resultSet.getInt(SQLPatinet.W_IS_INSURED);
        boolean czyUbezpieczony = false;
        if (czyUbezpieczonyInt == 1) czyUbezpieczony = true;
        String professionString = resultSet.getString(SQLPatinet.W_PROFESSION);
        Zawod zawod = Zawod.valueOf(professionString);

        return new Patient(
                id,
                imie,
                nazwisko,
                plec,
                czyUbezpieczony,
                zawod
        );
    }

    public void savePatient(Patient patient) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet generatedKeys = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = DaoUtil.prepareStatement(connection, SQLPatinet.getSavePatientQuery(patient), true);
            preparedStatement.executeUpdate();
            generatedKeys = preparedStatement.getGeneratedKeys();

            if (generatedKeys.next()) {
                int key = generatedKeys.getInt(1);
                patient.setId(key);
                System.out.println("Pacjent " + patient.getNazwisko() + " dodany do bazy. ID = " + key);
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement);
        }
    }

    public void deletePatient(Patient patient) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = DaoUtil.prepareStatement(connection, SQLPatinet.getDeletePatientQuery(patient), false);
            preparedStatement.executeUpdate();
            System.out.println("Pomyślnie usunięto: " + patient);
        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement);
        }
    }

    public void updatePatient(int id, Patient patient) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = DaoUtil.prepareStatement(connection, SQLPatinet.getUpdatePatientQuery(id, patient), false);
            preparedStatement.executeUpdate();
            System.out.println("Pomyślnie Edytowano pracownika o id: " + id);
        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement);
        }
    }


}
